<?php


namespace tencent;


class Map
{
    protected $Key = '';
    protected $Output = 'json';

    public function __construct($key)
    {
        $this->Key = $key;
    }

    //地址转坐标
    public function AddressToLocation($address, $result_type = '')
    {
        $url = 'https://apis.map.qq.com/ws/geocoder/v1/?address=' . $address . '&key=' . $this->Key . '&output=' . $this->Output;
        $result = $this->curl2get($url);
        $result = json_decode($result, true);
        if ($result_type == 'location') {
            if (!$result['status']) {
                return $result['result']['location'];
            } else {
                return $result;
            }
        }
        return $result;
    }

    /**坐标转地址
     * @param string $location //lat<纬度>,lng<经度>
     * @param string $poi_options //控制POI列表
     * @param int $get_poi //是否返回周边地点
     * @return array
     */
    public function LocationToAddress($location, $poi_options = '', $get_poi = 0)
    {
        $url = 'https://apis.map.qq.com/ws/geocoder/v1/?location=' . $location . '&key=' . $this->Key . '&get_poi=' . $get_poi . '&output=' . $this->Output;
        if ($get_poi && $poi_options) {
            $url .= '&poi_options=' . $poi_options;
        }
        $result = $this->curl2get($url);
        $result = json_decode($result, true);
        return $result;
    }

    /**矩阵距离计算
     * @param string $mode driving：驾车 walking：步行 bicycling：自行车
     * @param array $from [['lat' => '', 'lng' => '', 'heading' => ''],['lat' => '', 'lng' => '', 'heading' => ''],...]
     * @param array $to [['lat' => '', 'lng' => '',],['lat' => '', 'lng' => ''],...]
     * @return array
     * */
    public function DistanceCalculateMatrix($mode, $from, $to)
    {
        $url = 'https://apis.map.qq.com/ws/distance/v1/matrix?mode=' . $mode;
        $from_str = [];
        foreach ($from as $f) {
            if (!isset($f['lat']) || !isset($f['lng'])) {
                return ['status' => false, 'message' => '参数错误'];
            }
            $from_str[] = $f['lat'] . ',' . $f['lng'] . (isset($f['heading']) ? ',' . $f['heading'] : '');
        }
        $from_str = implode(';', $from_str);
        $to_str = [];
        foreach ($to as $f) {
            if (!isset($f['lat']) || !isset($f['lng'])) {
                return ['status' => false, 'message' => '参数错误'];
            }
            $to_str[] = $f['lat'] . ',' . $f['lng'];
        }
        $to_str = implode(';', $to_str);
        $url .= '&from=' . $from_str . '&to=' . $to_str . '&key=' . $this->Key;
        $result = $this->curl2get($url);
        return json_decode($result, true);
    }

    /**距离计算
     * @param array $from ['lat' => '', 'lng' => '']
     * @param array $to ['lat' => '', 'lng' => '']
     * @param string $mode driving：驾车 walking：步行 bicycling：自行车
     * @return array
     * */
    public function DistanceCalculate($from, $to, $mode = 'driving')
    {
        $from_str = $from['lat'] . ',' . $from['lng'];
        $to_str = $to['lat'] . ',' . $to['lng'];
        $url = 'https://apis.map.qq.com/ws/direction/v1/' . $mode . '/?from=' . $from_str . '&to=' . $to_str . '&output=json&callback=cb&key=' . $this->Key;
        $result = $this->curl2get($url);
        return json_decode($result, true);
    }

    /**区域搜索关键词显示相关地址
     * @param string $keyword
     * @param string $region
     * @param string $options
     * @return array
     * */
    public function getSearchAboutAddress($keyword, $region, $options)
    {
        $url = 'https://apis.map.qq.com/ws/place/v1/suggestion?keyword=' . $keyword . '&region=' . $region . '&key=' . $this->Key;
        foreach ($options as $k => $o) {
            $url .= '&' . $k . '=' . $o;
        }
        $result = $this->curl2get($url);
        return json_decode($result, true);
    }

    /**IP定位
     * @param string $ip
     * @return array
     * */
    public function ipToLocation($ip)
    {
        $url = 'https://apis.map.qq.com/ws/location/v1/ip?ip=' . $ip . '&key=' . $this->Key;
        $result = $this->curl2get($url);
        return json_decode($result, true);
    }

    //curl请求
    protected function curl2get($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        $data = curl_exec($curl);

        curl_close($curl);
        return $data;
    }

    public function setOutput($type)
    {
        $this->Output = $type;
        return $this;
    }

    //行政区划
    public function wxDistrict($code = '')
    {
        if ($code) {
            $url = 'https://apis.map.qq.com/ws/district/v1/getchildren?';
            $url .= 'id=' . $code . '&';
        } else {
            $url = 'https://apis.map.qq.com/ws/district/v1/list?';
        }
        $url .= 'key=' . $this->Key;
        $result = $this->curl2get($url);
        $result = json_decode($result, true);
        $district = [];
        if (!$result['status']) {
            foreach ($result['result'][0] as $s){
                $district[] = ['name' => $s['fullname'], 'location' => $s['location'], 'id' => $s['id']];
            }
        }
        return $district;
    }
}